package com.ruoyi.system.service.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.common.utils.Mysql;
import com.ruoyi.system.domain.TDrugInfo;
import com.ruoyi.system.service.ITDrugInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.TPrescriptionDrugInfo;
import com.ruoyi.system.mapper.TPrescriptionRecordsMapper;
import com.ruoyi.system.domain.TPrescriptionRecords;
import com.ruoyi.system.service.ITPrescriptionRecordsService;
import com.ruoyi.common.core.text.Convert;

/**
 * 处方信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-11
 */
@Service
public class TPrescriptionRecordsServiceImpl implements ITPrescriptionRecordsService 
{
    @Autowired
    private TPrescriptionRecordsMapper tPrescriptionRecordsMapper;

    /**
     * 查询处方信息
     * 
     * @param id 处方信息主键
     * @return 处方信息
     */
    @Override
    public TPrescriptionRecords selectTPrescriptionRecordsById(Long id)
    {
        return tPrescriptionRecordsMapper.selectTPrescriptionRecordsById(id);
    }

    /**
     * 查询处方信息列表
     * 
     * @param tPrescriptionRecords 处方信息
     * @return 处方信息
     */
    @Override
    public List<TPrescriptionRecords> selectTPrescriptionRecordsList(TPrescriptionRecords tPrescriptionRecords)
    {
        return tPrescriptionRecordsMapper.selectTPrescriptionRecordsList(tPrescriptionRecords);
    }

    /**
     * 新增处方信息
     * 
     * @param tPrescriptionRecords 处方信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTPrescriptionRecords(TPrescriptionRecords tPrescriptionRecords)
    {
        int rows = tPrescriptionRecordsMapper.insertTPrescriptionRecords(tPrescriptionRecords);
        insertTPrescriptionDrugInfo(tPrescriptionRecords);
        return rows;
    }

    /**
     * 修改处方信息
     * 
     * @param tPrescriptionRecords 处方信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTPrescriptionRecords(TPrescriptionRecords tPrescriptionRecords)
    {
        tPrescriptionRecordsMapper.deleteTPrescriptionDrugInfoByPrescriptionid(tPrescriptionRecords.getId());
        insertTPrescriptionDrugInfo(tPrescriptionRecords);
        return tPrescriptionRecordsMapper.updateTPrescriptionRecords(tPrescriptionRecords);
    }

    /**
     * 修改处方信息
     *
     * @param tPrescriptionRecords 处方信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTPrescriptionRecords1(TPrescriptionRecords tPrescriptionRecords)
    {
        return tPrescriptionRecordsMapper.updateTPrescriptionRecords1(tPrescriptionRecords);
    }


    /**
     * 批量删除处方信息
     * 
     * @param ids 需要删除的处方信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTPrescriptionRecordsByIds(String ids)
    {
        tPrescriptionRecordsMapper.deleteTPrescriptionDrugInfoByPrescriptionids(Convert.toStrArray(ids));
        return tPrescriptionRecordsMapper.deleteTPrescriptionRecordsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除处方信息信息
     * 
     * @param id 处方信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTPrescriptionRecordsById(Long id)
    {
        tPrescriptionRecordsMapper.deleteTPrescriptionDrugInfoByPrescriptionid(id);
        return tPrescriptionRecordsMapper.deleteTPrescriptionRecordsById(id);
    }

    @Override
    public int updateNumber(Long id) {
        return tPrescriptionRecordsMapper.updateNumber(id);
    }

    @Override
    public List<TPrescriptionRecords> selectCount(Long id) {
        return tPrescriptionRecordsMapper.selectCount(id);
    }

    /**
     * 新增处方的药品信息信息
     * 
     * @param tPrescriptionRecords 处方信息对象
     */
    public void insertTPrescriptionDrugInfo(TPrescriptionRecords tPrescriptionRecords)
    {
        List<TPrescriptionDrugInfo> tPrescriptionDrugInfoList = tPrescriptionRecords.getTPrescriptionDrugInfoList();
        Long id = tPrescriptionRecords.getId();
        if (StringUtils.isNotNull(tPrescriptionDrugInfoList))
        {
            List<TPrescriptionDrugInfo> list = new ArrayList<TPrescriptionDrugInfo>();
            for (TPrescriptionDrugInfo tPrescriptionDrugInfo : tPrescriptionDrugInfoList)
            {
                tPrescriptionDrugInfo.setPrescriptionid(id);
                list.add(tPrescriptionDrugInfo);
            }
            if (list.size() > 0)
            {
                tPrescriptionRecordsMapper.batchTPrescriptionDrugInfo(list);
            }
        }
    }

    @Override
    public int getTotalCost(TPrescriptionRecords tPrescriptionRecords) {
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format1 = sim.format(tPrescriptionRecords.getCrtime());
        int amount = 0;
        Connection conn = Mysql.getConn();
        Statement stmt = null;
        try {
            // 执行查询
            stmt = conn.createStatement();
            String sql;
            sql = " SELECT " +
                    " SUM(drug_price*(counts*days-refund_counts)) AS amount  " +
                    " FROM " +
                    " t_prescription_drug_info " +
                    " WHERE 1=1 AND prescriptionid IN ( " +
                    "  SELECT id FROM t_prescription_records WHERE NAME='"+tPrescriptionRecords.getName()+"' AND crtime='"+format1+"' " +
                    "  ) ";

            System.out.println(sql);

            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next()){
                amount = rs.getInt("amount");
            }
            rs.close();
        }  catch (Exception e) {
            throw new RuntimeException(e);
        }
        return amount;
    }

    @Override
    public List<Map<String, Object>> getDrugList(TPrescriptionRecords tPrescriptionRecords) {
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format1 = sim.format(tPrescriptionRecords.getCrtime());
        List<Map<String, Object>> list= new ArrayList<>();
        Connection conn = Mysql.getConn();
        Statement stmt = null;
        try {
            // 执行查询
            stmt = conn.createStatement();
            String sql;
            sql = " SELECT " +
                    " drug_name,drug_specs,drug_price,counts*days-refund_counts AS counts,drug_price*(counts*days-refund_counts) AS money " +
                    " FROM " +
                    " t_prescription_drug_info " +
                    " WHERE 1=1 AND prescriptionid IN ( " +
                    "  SELECT id FROM t_prescription_records WHERE NAME='"+tPrescriptionRecords.getName()+"' AND crtime='"+format1+"'  " +
                    "  ) ";
            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next()){
                Map<String, Object> map = new HashMap<>();
                String drugName = rs.getString("drug_name");
                String drugSpecs = rs.getString("drug_specs");
                double drugPrice = rs.getDouble("drug_price");
                int counts = rs.getInt("counts");
                double money = rs.getDouble("money");
                map.put("drugName",drugName);
                map.put("drugSpecs",drugSpecs);
                map.put("drugPrice",drugPrice);
                map.put("counts",counts);
                map.put("money",money);
                list.add(map);
            }
            rs.close();
        }  catch (Exception e) {
            throw new RuntimeException(e);
        }
        return list;
    }
}
